import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:smart_devices/bruno/src/components/toast/brn_toast.dart';
import 'package:smart_devices/model/repair.dart';

class MineRepairLogic extends GetxController {
  RxInt tabIndex = RxInt(0);

  RxList<String> tabs = RxList(['本周', '上周', '30天内', '自定义']);

  RxString option = RxString('');

  RxList<String> extensions = RxList([]);

  RxList<RepairModel> pageList = RxList([]);

  final ScrollController scrollController = ScrollController();

  RxBool isLoading = RxBool(false);

  @override
  void onInit() {
    super.onInit();
    option.value = '''
      {
  color: [
    '#506ABC',
    '#91CC75',
    '#FAC858',
    '#EE6666',
    '#70BBD8',
    '#02A7F0',
    '#E7E7E7'
  ],
  tooltip: {
    show: false,
    trigger: 'item',
    formatter: '{a}: {b}<br />{c}：{d}%'
  },
  legend: {
    top: '10%',
    right: '5%',
    orient: 'vertical',
    itemHeight: 15,
    itemWidth: 15,
    textStyle: {
      fontFamily: '苹方 常规, 苹方, sans-serif',
      fontWeight: 400,
      fontStyle: 'normal',
      fontSize: '12px',
      color: '#606266',
      textAlign: 'center'
    },
    formatter: function(name) {
      for (var row of data) {
        if (row.name == name) {
          return row.name + '  ' + row.value;
        }
      }
      return name;
    }
  },
  series: [
    {
      name: '统计',
      type: 'pie',
      radius: ['50%', '70%'],
      center: ['50%', '50%'],
      avoidLabelOverlap: false,
      top: 'top',
      left: 'left',     
      width: 240,
      height: 240,
      label: {
        show: true,
        position: 'center',
        fontSize: 28,
        fontWeight: 700,
        color: '#495060',
        fontStyle: 'normal',
        fontFamily: '苹方 粗体, 苹方 常规, 苹方, sans-serif',
        formatter: function (params) {
          return params.data.value + '台';
        }
      },
      emphasis: {
        show: false,
        label: {
          show: false,
          fontSize: 40,
          fontWeight: 'bold'
        }
      },
      labelLine: {
        show: false
      },
      data: [
        { value: 5, name: '待审核' },
        { value: 5, name: '驳回' },
        { value: 5, name: '为派工' },
        { value: 5, name: '待接单' },
        { value: 5, name: '维修中' },
        { value: 5, name: '待验证' },
        { value: 5, name: '已完成' }
      ]
    }
  ]
}
    ''';
    extensions.add(r'''
      var data = [
        { value: 5, name: '待审核' },
        { value: 5, name: '驳回' },
        { value: 5, name: '为派工' },
        { value: 5, name: '待接单' },
        { value: 5, name: '维修中' },
        { value: 5, name: '待验证' },
        { value: 5, name: '已完成' }
      ]      
    ''');
    initData();
    scrollController.addListener(() {
      if (!isLoading.value &&
          scrollController.position.pixels >= scrollController.position.maxScrollExtent) {
        isLoading.value = true;
        loadMoreData();
      }
    });
  }

  @override
  onClose() {
    super.onClose();
    scrollController.dispose();
  }

  initData() {
    for (var i = 0; i < 10; i++) {
      pageList.add(RepairModel(
          title: '电梯设备维修02',
          orderNo: 'BX-2023041648',
          name: '电梯设备',
          code: 'SB0792789',
          location: '生产车间二层',
          degree: getDegreeByIndex(i),
          addTime: '2023.11.14 08:32:31',
          status: getStatusByIndex(i)));
    }
  }

  Future<void> loadMoreData() {
    return Future.delayed(const Duration(seconds: 1), () {
      isLoading.value = false;
      initData();
    });
  }

  RepairDegreeEnum getDegreeByIndex(int index) {
    var mode = index % RepairDegreeEnum.values.length;
    switch (mode) {
      case 0:
        return RepairDegreeEnum.slow;
      case 1:
        return RepairDegreeEnum.mid;
      default:
        return RepairDegreeEnum.fast;
    }
  }

  RepairStatusEnum getStatusByIndex(int i) {
    var mode = i % RepairStatusEnum.values.length;
    switch (mode) {
      case 0:
        return RepairStatusEnum.audit;
      case 1:
        return RepairStatusEnum.reject;
      case 2:
        return RepairStatusEnum.completed;
      case 3:
        return RepairStatusEnum.repair;
    }
    return RepairStatusEnum.audit;
  }

  Future<void> onRefresh(BuildContext context) {
    return Future.delayed(const Duration(seconds: 1), () {
      BrnToast.show('当前已是最新数据', context,
          gravity: BrnToastGravity.bottom, duration: const Duration(seconds: 2), radius: 10);
    });
  }
}
